package br.ufrgs.ga.model;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Population {

	/**
	 * Population size.
	 */
	public static final int POPULATION_SIZE = 100;
	
	/**
	 * Population mutation frequence.
	 */
	public static final int MUTATION_FREQUENCE = Population.POPULATION_SIZE/10;
	
	/**
	 * Population reproduction frequence.
	 */
	public static final int REPRODUCTION_FREQUENCE = Population.POPULATION_SIZE/2;
	
	
	private List<Individual> population = new LinkedList<Individual>();
	
	public Individual getMostAdaptedIndividual() {
		// Ascendent sort
		Collections.sort(population);
		return population.get(population.size() - 1);
	}
	
	public void add(Individual chromosome) {
		this.population.add(chromosome);
	}
	
	public void addAll(List<Individual> individuals) {
		this.population.addAll(individuals);
	}
	
	public void remove(Individual chromosome) {
		this.population.remove(chromosome);
	}
	
	public int size() {
		return population.size();
	}
	
	public List<Individual> getSubListOfWorstIndivuduals(int size) {
		if (size > population.size())
			size = population.size();
		else if (size < 0)
			size = 0;
		
		return population.subList(0, size);
	}
	
	public List<Individual> getSubListOfBestIndivuduals(int size) {
		if (size > population.size())
			size = population.size();
		else if (size < 0)
			size = 0;
		
		return population.subList(size, population.size());
	}
	
	public Individual getIndividualAt(int index) {
		if (index >= population.size()) {
			index = population.size() - 1;
		} else if(index < 0) {
			index = 0;
		}
		
		return population.get(index);
	}
}
